package com.example.testnews;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.example.testnews.HttpUtil.HttpUtil;

import java.io.IOException;

public class Test01 extends AppCompatActivity {
    private Button button;
    private TextView textView;
//    Handler handler = new Handler() {
//        @SuppressLint("HandlerLeak")
//        @Override
//        public void handleMessage(Message msg) {
//            super.handleMessage(msg);
//            switch (msg.what) {
//                case 1:
//                    textView.setText((String) msg.obj);
//                    break;
//                case 2:
//                    textView.setText((String)(msg.obj));
//                    break;
//            }
//        }
//    };

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.text001);
        button=findViewById(R.id.button1);
        textView=findViewById(R.id.textView1);
        button.setOnClickListener(view -> {
            new Thread(){
                @Override
                public void run() {
                    super.run();
                    try {
                        String result = HttpUtil.sendOkHttpRequest("https://api.tianapi.com/topnews/index?key=d4f64db4ff1bbae9134d5a71b9d98c20");
                        textView.setText(result);
//                        Message msg = Message.obtain();
//                        msg.what = 1;
//                        msg.obj = result;
//                        handler.sendMessage(msg);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }.start();
        });
    }
}
